package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Employee;
import com.itheima.bean.PageBean;
import com.itheima.bean.PageParams;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author hebing
 * @version 1.0
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper em;

    //登录
    @Override
    public Employee employeeLogin(Employee employee) {
        //对密码进行加密
        String md5Password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //1.构建查询条件对象
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
        lambdaQueryWrapper.eq(Employee::getPassword, md5Password);
        //添加判定 如果状态是1才可以登录
        lambdaQueryWrapper.eq(Employee::getStatus,1);
        //2.调用mapper查询
        Employee loginEmployee = em.selectOne(lambdaQueryWrapper);
        return loginEmployee;
    }

    //添加员工
    @Override
    public int add(Employee employee) {
        int rows = em.insert(employee);
        return rows;
    }

    /**
     * 分页查询
     * @param params
     * @return
     */
    @Override
    public PageBean<Employee> findPage(PageParams params) {
        //构建查询条件
        IPage iPage=new Page();
        //设置当前页码
        iPage.setCurrent(params.getPage());
        //设置查询总条数
        iPage.setSize(params.getPageSize());
        //设置分页查询条件
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        lqw.like(params.getName()!=null,Employee::getName,params.getName());
        //调用dao执行查询
        IPage selectPage= em.selectPage(iPage, lqw);
        //封装查询结果返回
        PageBean<Employee> pageBean = new PageBean<>();
        //封装查询总条数
        pageBean.setTotal((int) selectPage.getTotal());
        //封装查询当前页数据
        pageBean.setRecords(selectPage.getRecords());
        //查看
        System.out.println(pageBean);
        return pageBean;
    }

    /**
     * 修改数据
     * @param request
     * @param employee
     * @return
     */
    @Override
    public int update(HttpServletRequest request, Employee employee) {
        //根据id查询数据库
        Employee e = em.selectById(employee.getId());
        System.out.println("e:"+e);
        if (e.getStatus() == 0) {
            e.setStatus(1);
        } else {
            e.setStatus(0);
        }
        System.out.println("状态码的值为:"+e.getStatus());
        //获取当前登录人员信息
        Long empId = (Long) request.getSession().getAttribute("employee");
        //设置修改用户
        //e.setUpdateUser(empId);
        //设置修改时间 当前系统时间
        //e.setUpdateTime(LocalDateTime.now());
        //根据提供的信息进行修改
        int rows = em.updateById(e);

        return rows;
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Override
    public Employee selectById(Long id) {
        //调用dao方法操作数据库得到结果
        Employee employee = em.selectById(id);
        return employee;
    }

    /**
     * 添加
     * @param employee
     * @return
     */
    @Override
    public int addUpdate(Employee employee) {
        //调用dao进行添加数据
        int rows = em.updateById(employee);
        return rows;
    }


}
